package com.huawei.hwid.core.encrypt;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.secure.android.common.encrypt.aes.AesCbc;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class a {
    private static String a = "";

    public static void a(Context context) {
        a = com.huawei.hwid.core.c.a.a(context).a("rkey", "");
    }

    @SuppressLint({"TrulyRandom"})
    public static String a(Context context, String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            if (TextUtils.isEmpty(a)) {
                a = com.huawei.hwid.core.d.a.b();
                com.huawei.hwid.core.c.a.a(context).a("rkey", a);
            }
            return AesCbc.encrypt(str, f(context, a));
        } catch (RuntimeException e) {
            e.d("AES128_CBC", "aes cbc encrypter data error", true);
            return "";
        } catch (Exception e2) {
            e.d("AES128_CBC", "aes cbc encrypter data error", true);
            return "";
        }
    }

    public static String b(Context context, String str) {
        if (TextUtils.isEmpty(a)) {
            return d(context, str);
        }
        return c(context, str);
    }

    public static String c(Context context, String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            return AesCbc.decrypt(str, f(context, a));
        } catch (RuntimeException e) {
            e.d("AES128_CBC", "Random decrypter data error RuntimeException", true);
            return d(context, str);
        } catch (Exception e2) {
            e.d("AES128_CBC", "Random decrypter data error", true);
            return d(context, str);
        }
    }

    private static String d(Context context, String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            return AesCbc.decrypt(str, b(context));
        } catch (RuntimeException e) {
            e.d("AES128_CBC", "decrypter data error RuntimeException", true);
            return e(context, str);
        } catch (Exception e2) {
            e.d("AES128_CBC", "decrypter data error", true);
            return e(context, str);
        }
    }

    private static String e(Context context, String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            return a(context, str, c(context));
        } catch (RuntimeException e) {
            e.d("AES128_CBC", "OLDKEY decrypter data error again RuntimeException", true);
            return "";
        } catch (Exception e2) {
            e.d("AES128_CBC", "OLDKEY decrypter data error again", true);
            return "";
        }
    }

    private static String a(Context context, String str, byte[] bArr) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        Key secretKeySpec = new SecretKeySpec(bArr, "AES");
        Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
        String a = a(str);
        String b = b(str);
        if (TextUtils.isEmpty(a) || TextUtils.isEmpty(b)) {
            e.b("AES128_CBC", "ivParameter or encrypedWord is null", true);
            return "";
        }
        instance.init(2, secretKeySpec, new IvParameterSpec(b.b(a)));
        return new String(instance.doFinal(b.b(b)), "UTF-8");
    }

    public static byte[] b(Context context) {
        byte[] b = b.b(HwAccountConstants.a());
        byte[] b2 = b.b(b.c());
        return a(a(a(a(b, -4), b2), 6), b.b(com.huawei.hwid.core.d.a.a()));
    }

    private static byte[] f(Context context, String str) {
        byte[] b = b.b(HwAccountConstants.a());
        byte[] b2 = b.b(b.c());
        return a(a(a(a(b, -4), b2), 6), b.b(com.huawei.hwid.core.d.a.b(context, str)));
    }

    private static byte[] c(Context context) {
        byte[] b = b.b(HwAccountConstants.a());
        byte[] b2 = b.b(b.c());
        return a(a(a(b, b2), b.b(com.huawei.hwid.core.d.a.a())));
    }

    private static byte[] a(byte[] bArr) {
        if (bArr != null) {
            for (int i = 0; i < bArr.length; i++) {
                bArr[i] = (byte) (bArr[i] >> 2);
            }
        }
        return bArr;
    }

    private static byte[] a(byte[] bArr, int i) {
        if (bArr != null) {
            for (int i2 = 0; i2 < bArr.length; i2++) {
                if (i < 0) {
                    bArr[i2] = (byte) (bArr[i2] << (-i));
                } else {
                    bArr[i2] = (byte) (bArr[i2] >> i);
                }
            }
        }
        return bArr;
    }

    private static byte[] a(byte[] bArr, byte[] bArr2) {
        byte[] bArr3 = null;
        if (!(bArr == null || bArr2 == null)) {
            int length = bArr.length;
            if (length == bArr2.length) {
                bArr3 = new byte[length];
                for (int i = 0; i < length; i++) {
                    bArr3[i] = (byte) (bArr[i] ^ bArr2[i]);
                }
            }
        }
        return bArr3;
    }

    private static String a(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(str.substring(6, 12));
            stringBuffer.append(str.substring(16, 26));
            stringBuffer.append(str.substring(32, 48));
            return stringBuffer.toString();
        } catch (RuntimeException e) {
            e.d("AES128_CBC", "RuntimeException", true);
            return "";
        } catch (Exception e2) {
            e.d("AES128_CBC", "Exception", true);
            return "";
        }
    }

    private static String b(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(str.substring(0, 6));
            stringBuffer.append(str.substring(12, 16));
            stringBuffer.append(str.substring(26, 32));
            stringBuffer.append(str.substring(48));
            return stringBuffer.toString();
        } catch (RuntimeException e) {
            e.d("AES128_CBC", "RuntimeException", true);
            return "";
        } catch (Exception e2) {
            e.d("AES128_CBC", "Exception", true);
            return "";
        }
    }
}
